<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/19
 * Time: 14:13
 */

/**
 * Created by wanglu on 2017/7/19.
 */
class Version extends AdminBase
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('user_service');
        $this->load->service('project_service');
        $this->load->service('lang_service');
        $this->load->service('key_service');
        $this->load->service('project_content_service');

        $this->load->service('Project_version_service');
    }

    public function submitversion() {
        $pid = $this->input->post("pid");
        $remark = $this->input->post("remark");
        $lang_ids = $this->input->post("lang_ids");
        $lang_ids = json_decode($lang_ids, true);

        $project_user_relation = $this->project_service->get_project_user_relation($pid, $this->loginUser['id']);


        if ( $this->project_version_service->submit($this->loginUser, $pid, $lang_ids, $remark, $project_user_relation)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }


     public function project_version() {

         if ($this->is_post()) {

             $pid = $this->input->post("pid");
             $search = $this->input->post("search");
             $order = $this->input->post("order");
             $order_by_createtime = $order[0]["dir"];
             $searchValue = $search['value'];

             $start = $this->input->post("start");
             $length = $this->input->post("length");

             $data = $this->project_version_service->get_project_version($start, $length, $pid, $order_by_createtime);
             foreach ($data['data'] as $key => $value) {
                 $data['data'][$key]["createtime"] = TimeUtil::showtime($data['data'][$key]["createtime"], TimeUtil::TIME_ZONE_GMT8);
                 $lang_version_info =  $data['data'][$key]['lang_version_info'];

                 $lang_version_info = json_decode($lang_version_info, true);
                 $submit_langs = json_decode($data['data'][$key]['submit_lang'], true);

                 $lang_version_html = "";
                 $submit_langs_html = "";

                 foreach ($lang_version_info as $lf) {
                     $lang_version_html .= $lf['name'] . ":" .$lf['version'] . "&nbsp;&nbsp;|&nbsp;&nbsp;";
                 }

                 foreach ($submit_langs as $sl) {
                     $submit_langs_html .= $sl['name'] . "|";
                 }





                 $data['data'][$key]['lang_version_info'] = $lang_version_html;

                 $data['data'][$key]['submit_lang'] = $submit_langs_html;
             }


             
             echo json_encode($data);
         } else {

             $pid = $this->input->get("pid");
             $project = $this->project_service->get_project_by_id($pid);
             $this->showView("tpl_project_version", array('project' => $project, ));
         }

     }
    


}